//create assign pbx;

//create new window popup
/*
Ext.define('CDR.widget.AssignPBX', {
	extend: 'Ext.window.Window',
	alias : 'widget.assignpbx',
	title: CDR.I18N['organization.assign-pbx.form.title'],
	closable: true,
	closeAction: 'hide',
	width: 375,
	height: 200,
	bodyStyle: 'padding: 5px;',
	targetObject: null,
	type: null,
	url: '',
	addFieldValues : [],
	//set default value
	editData: function(data){
		var name = this.form.down('textfield');
		name.setValue(data.name);
		var description = this.form.down('textareafield');
		description.setValue(data.description);
	},
	initComponent: function() {
       this.callParent();
	   this.form = Ext.createWidget('form',{
			url: this.url,
			bodyStyle: 'padding: 5px;',
			items:[{
				xtype: 'textfield',
				anchor: '98%',
				allowBlank: false,
				fieldLabel: CDR.I18N['profile.provider.form.name'],
				name: 'name'
			}, {
				xtype: 'textareafield',
				fieldLabel: CDR.I18N['profile.provider.form.description'],
				anchor: '98%',
				name: 'description'
			}],
			buttons: [{
				text: CDR.I18N['button.save'],
				formBind: true, 
				handler: function() {
					var win = this.up('window');
					if(win.addFieldValues.length > 0){
						Ext.each(win.addFieldValues, function(i){
							var hf = this.down('hidden[name=' + i.name +']');
							if(hf) {
								hf.setValue(i.value);
							}
						},this.up('form'));
					}
					var form = this.up('form').getForm();
					if (form.isValid()) {
						form.submit({
							success: function(form, action) {
							   Ext.Msg.alert('Success', action.result.msg);
							   win.hide();
							   form.reset();
							   if(win.targetObject && win.targetObject.submitOk) {
									win.targetObject.submitOk(action.result, win.type);
							   }
							},
							failure: function(form, action) {
								Ext.Msg.alert('Failed', action.result.msg);
							}
						});
					}
				}
			},{
				text: CDR.I18N['button.cancel'],
				handler: function() {
					var win = this.up('window');
					win.hide();
				}
				
			}]
		});
		var addFields = this.initialConfig.addFields;
		if(addFields){
			Ext.each(addFields, function (item){
				this.add(item);
			}, this.form);
		}
		this.add(this.form);
    },
});

*/
Ext.define('CDR.widget.Site', {
	extend: 'Ext.container.Container',
	layout:'border',
	initComponent: function() {
		var me = this;
		me.callParent();
		me.west = Ext.create('widget.sitetree', {
			region:'west',
			collapsible: false,
			title:CDR.I18N['organization.tree.title'],
			split: true,
			minWidth: 250,
			maxWidth: 250
		});
		
		me.west.userView = Ext.createWidget('userview', {region: 'center'});
		
		me.center = Ext.create('widget.container', {
			region:'center',
			layout:'border',
			border: false
		});
		
		me.center.userDetail = Ext.create('widget.container', {
			region:'south',
			height:'40%',
			layout:'fit',
			split: true
		});
		
		//create user form
		me.center.userDetail.userForm = Ext.createWidget('userassign', {region: 'center', url:'assign-user', treeView:me.west});
		//create pbx form
		me.center.userDetail.pbxForm = Ext.createWidget('pbxassign', {region: 'center', url:'assign-pbx', treeView:me.west});
		//link from user form to user view
		me.center.userDetail.userForm.userView = me.west.userView;
		me.center.userDetail.pbxForm.userView = me.west.userView;
		//link from userview to user form and pbx form
		me.west.userView.userForm = me.center.userDetail.userForm;
		me.west.userView.pbxForm = me.center.userDetail.pbxForm;
		me.west.userView.userDetail = me.center.userDetail;
		me.west.userView.treeView = me.west;
		

		me.center.user = Ext.create('widget.panel', {
			region:'center',
			layout: 'fit',
			padding: '5 5 5 0',
			tbar: [{
				text:CDR.I18N['organization.toolbar.assign-pbx'],
				iconCls: 'icon-pbx',
				handler: function(){
					//get data from server
					Ext.Ajax.request({
						scope: this,
						async: false,
						url: 'list-assign-pbx',
						success: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							this.center.userDetail.pbxForm.getForm().reset();
							this.center.userDetail.pbxForm.editData(obj, this.west);
						}
					});
					this.center.userDetail.removeAll(false);
					this.center.userDetail.add(this.center.userDetail.pbxForm);
				},
				scope: me
			},'-',{
				text: CDR.I18N['organization.toolbar.unassign-pbx'],
				iconCls: 'icon-delete',
				handler: function(){
					//get all selected nodes
					var selectedNodes = this.west.userView.getSelectionModel().getSelection();
					var strId = "";
					Ext.each(selectedNodes, function(item){
						if(item.data.type == "PBX"){
							strId += item.data.obId + ",";
						}
					});
					//get data from server
					if(strId != ""){
						Ext.Ajax.request({
							scope: this,
							method:'POST',
							async: false,
							url: 'un-assign-pbx',
							params: {"pbxIds":strId},
							success: function(response, opts) {
								var obj = Ext.decode(response.responseText);
								this.west.reloadSameItemClick();
								Ext.Msg.alert('Success', obj.msg);
							},
							failure: function(response) {
								var obj = Ext.decode(response.responseText);
								Ext.Msg.alert('Failed', obj.msg);
							}
						});
					}
				},
				scope: me
			},'-',{
				text: CDR.I18N['organization.toolbar.assign-user'],
				iconCls: 'icon-user-add',
				handler: function(){
					//get data from server
					Ext.Ajax.request({
						scope: this,
						async: false,
						url: 'list-assign-user',
						success: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							this.center.userDetail.userForm.getForm().reset();
							this.center.userDetail.userForm.editData(obj, this.west);
						}
					});
					this.center.userDetail.removeAll(false);
					this.center.userDetail.add(this.center.userDetail.userForm);
				},
				scope: me
			},'-',{
				text: CDR.I18N['organization.toolbar.unassign-user'],
				iconCls: 'icon-delete',
				handler: function(){
					//get all selected nodes
					var selectedNodes = this.west.userView.getSelectionModel().getSelection();
					var strId = "";
					Ext.each(selectedNodes, function(item){
						if(item.data.type == "USER"){
							strId += item.data.obId + ",";
						}
					});
					//get data from server
					if(strId != ""){
						Ext.Ajax.request({
							scope: this,
							method:'POST',
							async: false,
							url: 'un-assign-user',
							params: {"userIds":strId},
							success: function(response, opts) {
								var obj = Ext.decode(response.responseText);
								this.west.reloadSameItemClick();
								Ext.Msg.alert('Success', obj.msg);
							},
							failure: function(response) {
								var obj = Ext.decode(response.responseText);
								Ext.Msg.alert('Failed', obj.msg);
							}
						});
					}
				},
				scope: me
				
			},'-','',{
				xtype: 'textfield',
				fieldLabel:CDR.I18N['label.search'],
				iconCls: 'icon-user-add',
				labelWidth: 45,
				listeners: {
					'change': function(me, newVal, oldVal){
						this.west.userView.store.clearFilter();
						var chooseType = this.center.user.down('combobox[name=chooseType]').getValue();
						this.west.userView.store.filter(new Ext.util.Filter({
								filterFn: function(item) {
									var searchtest,namematch, typematch;
									//filter on type
									if(chooseType == 'ALL'){
										typematch = true;
									} else {
										typematch = item.data.type == chooseType;
									}
									var escapere = Ext.String.escapeRegex;
									searchtest = new RegExp(escapere(newVal), 'i');
									namematch = searchtest.test(item.data.name);
									return typematch && namematch;
								}
							})
						);
					},
					scope: me
				}
			},'-','',{
				xtype: 'combobox',
				fieldLabel:CDR.I18N['organization.toolbar.view'],
				labelWidth: 30,
				value:'ALL',
				store: Ext.create('Ext.data.ArrayStore', {
					fields: ['value', 'key'],
					data : [['ALL', 'All'], ['USER', 'User'], ['PBX', 'PBX']]
				}),
				displayField: 'key',
				valueField: 'value',
				typeAhead: true,
				queryMode: 'local',
				triggerAction: 'all',
				selectOnFocus: true,
				name:'chooseType',
				width: 135,
				iconCls: 'no-icon',
				listeners:{
					'change':function(me, newVal, oldVal){
						var filter = this.west.userView.store;
						filter.clearFilter();
						filter.filter(new Ext.util.Filter({
								filterFn: function(item) {
									var searchtest,namematch;
									var escapere = Ext.String.escapeRegex;
									searchtest = new RegExp(escapere(newVal), 'i');
									namematch = searchtest.test(item.data.type);
									if(newVal === 'ALL'){
										filter.clearFilter();
										return true;
									}
									return namematch;
								}
							})
						);
					},
					scope: me
				}
			}],
			items:[me.west.userView]
		});
		
		me.center.add(me.center.userDetail, me.center.user);
		me.add(me.west, me.center);
	}
});

CDR.widget.Site.NODE_SITE = 'site';
CDR.widget.Site.NODE_PBK = 'pbk';